#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "queue.h"

static bool AddItem(Item temp, Queue *pline);
static bool DeleteItem(Item temp, Queue *pline);

void handleNodeFunction(Node *pnode);

int main(int argc, char const *argv[])
{
    Queue line;
    Item temp = 0;

    char ch;
    InitializeQueue(&line);
    puts("Testing Queue! Type 'a' to add a value,type 's' to Traverse all the Node in Customer-Function ,");
    puts("type 'd' to drop a value, and type 'q' to exit!");
    while ((ch = getchar()) != 'q')
    {
        //忽略其他的输入
        if (ch != 'a' && ch != 'd' && ch != 's')
        {
            continue;
        }
        if (ch == 'a')
        {
            printf("a selected!\nInteger to add: ");
            AddItem(temp, &line);
        }
        else if (ch == 'd')
        {
            printf("d selected!\n");
            DeleteItem(temp, &line);
        }
        else if (ch == 's')
        {
            printf("s selected!\n");
            void (*Function)(Node *) = handleNodeFunction;
            QueueTraverse(&line, Function);
        }

        printf("%d items in queue\n", QueueItemCount(&line));
        printf("Type a to add, s to traverse, d to delete, q to quit: ");
    }
    puts("Bye!\n");

    return 0;
}

/**
 * @brief 添加项目的封装类,利用queue.h中封装好的接口进行二次封装
 * 
 * @param temp Integer
 * @param line 队列指针
 * @return true 
 * @return false 
 */
static bool AddItem(Item temp, Queue *pline)
{
    scanf("%d", &temp);
    if (!QueueIsFull(pline))
    {
        if (AddItemFromTail(temp, pline))
        {
            puts("Add success! ");
            return true;
        }
        else
        {
            puts("Add Fail! ");
            return false;
        }
    }
    else
    {
        puts("Add FAIL! Queue is full! ");
        return false;
    }
}

/**
 * @brief 删除项目的封装类,利用queue.h中封装好的接口进行二次封装
 *
 * @param temp 传入新建的空Integer值,作为返回值
 * @param pline 队列指针
 * @return true
 * @return false
 */
static bool DeleteItem(Item temp, Queue *pline)
{
    if (QueueIsEmpty(pline))
    {
        puts("Nothing to delete!");
        return false;
    }
    else
    {
        DeleteItemFromTail(&temp, pline);
        printf("Success! Removing %d from queue! ", temp);
        return true;
    }
}

/**
 * @brief 用户自建函数,利用QueueTraverse中的遍历结构,传递这个函数代替处理遍历Node(类似于 快速排序qsort() 函数结构)
 * 
 * @param pnode 这个参数无效! 因为接口会占用pnode进行Node指针传递(会覆盖指针)
 */
void handleNodeFunction(Node *pnode)
{
    unsigned int temp = 0;
    temp = pnode->item;
    printf("Function is using to display all the Node, result is %d \n", temp);
}